--local names = {}
local isExec = true

function run()
    local sql = "select id,plant_gate_code from basic_plant bp ;"
    local dataTable = DataTable(sql)
    if (type(dataTable) ~= "table") then
        print(dataTable)
        return
    end

    for k, v in ipairs(dataTable) do
        print(table.concat(v, ","))
        update(v[1], v[2])
    end

    -- for k, v in pairs(names) do
    --     print(k .. ","..v)
    -- end
end

function makeSQL(sql, code, id, name)
    --names[code .. "_PT_NPPS_W_D_TOL_M"] = name .. "日发电量"
    --names[code .. "_PT_NPPS_W_ED_TOL_M"] = name .. "日上网电量"
    return string.format(sql, code, id, name)
end

function update(id, code)
    if (code == nil) then return end
    --names[code .. "_PT_NPPS_W_D_TOL_M"] = "日发电量"
    --names[code .. "_PT_NPPS_W_ED_TOL_M"] = "日上网电量"
    print(id, code)
    local sql = "update basic_staging set staging_gate_code='%s' where plant_id=%d and staging_name like '%%%s%%'"
    local updateSQL = makeSQL(sql, code .. "1", id, "一期")
    print(updateSQL)
    if (isExec) then Exec(updateSQL) end
    updateSQL = makeSQL(sql, code .. "1", id, "一期")
    print(updateSQL)
    if (isExec) then Exec(updateSQL) end
    updateSQL = makeSQL(sql, code .. "2", id, "二期")
    print(updateSQL)
    if (isExec) then Exec(updateSQL) end
    updateSQL = makeSQL(sql, code .. "3", id, "三期")
    print(updateSQL)
    if (isExec) then Exec(updateSQL) end
    updateSQL = makeSQL(sql, code .. "4", id, "四期")
    print(updateSQL)
    if (isExec) then Exec(updateSQL) end
    updateSQL = makeSQL(sql, code .. "5", id, "五期")
    print(updateSQL)
    if (isExec) then Exec(updateSQL) end
    updateSQL = makeSQL(sql, code .. "6", id, "六期")
    print(updateSQL)
    if (isExec) then Exec(updateSQL) end
    updateSQL = makeSQL(sql, code .. "1", id, "A")
    print(updateSQL)
    if (isExec) then Exec(updateSQL) end
    updateSQL = makeSQL(sql, code .. "2", id, "B")
    print(updateSQL)
    if (isExec) then Exec(updateSQL) end
    updateSQL = makeSQL(sql, code .. "3", id, "C")
    print(updateSQL)
    if (isExec) then Exec(updateSQL) end
    updateSQL = makeSQL(sql, code .. "4", id, "D")
    print(updateSQL)
    if (isExec) then Exec(updateSQL) end
    updateSQL = makeSQL(sql, code .. "5", id, "E")
    print(updateSQL)
    if (isExec) then Exec(updateSQL) end
    updateSQL = makeSQL(sql, code .. "6", id, "F")
    print(updateSQL)
    if (isExec) then Exec(updateSQL) end
end

run()
